package ldh.im.monitor.controller.sub;

import com.google.common.eventbus.Subscribe;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import ldh.im.dto.business.monitor.MonitorSummaryInfo;
import ldh.im.fxbase.util.FxThreadUtil;
import ldh.im.fxbase.util.RpcUtil;
import ldh.im.monitor.util.Param;
import ldh.rpc.TerminalType;
import ldh.rpc.cache.CacheService;

import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.function.Function;
import java.util.stream.Collectors;

public class SummaryController implements Initializable {

    @FXML private Label connectionNumLabel;
    @FXML private Label userNumLabel;
    @FXML private Label userPcNumLabel;
    @FXML private Label userAndroidNumLabel;
    @FXML private Label userIosNumLabel;
    @FXML private Label userPadNumLabel;
    @FXML private Label userIPadNumLabel;
    @FXML private Label userWebNumLabel;
    @FXML private Label userHmNumLabel;

    @Subscribe
    public synchronized void summaryInfo(MonitorSummaryInfo monitorSummaryInfo) {
        show(monitorSummaryInfo);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        CacheService cacheService = RpcUtil.getRpcBootstrap().getRpcConfig().getCacheService();
        MonitorSummaryInfo monitorSummaryInfo = (MonitorSummaryInfo) cacheService.getCache(Param.CACHE_MONITOR_SUMMARY);
        if (monitorSummaryInfo == null) {
            monitorSummaryInfo = new MonitorSummaryInfo();
            Map<TerminalType, Integer> map = new HashMap<>();
            for (TerminalType terminalType : TerminalType.values()) {
                map.put(terminalType, 0);
            }
            monitorSummaryInfo.setConnectionTotal(0);
            monitorSummaryInfo.setTotal(0);
            monitorSummaryInfo.setConnectionTotalMap(map);
        } else {
            for (TerminalType terminalType : TerminalType.values()) {
                Integer id = monitorSummaryInfo.getConnectionTotalMap().get(terminalType);
                if (id == null) {
                    monitorSummaryInfo.getConnectionTotalMap().put(terminalType, 0);
                }
            }
        }
        show(monitorSummaryInfo);
    }

    private void show(MonitorSummaryInfo monitorSummaryInfo) {
        FxThreadUtil.runLater(()->{
            connectionNumLabel.setText(monitorSummaryInfo.getConnectionTotal()+"");
            userNumLabel.setText(monitorSummaryInfo.getTotal() + "");

            monitorSummaryInfo.getConnectionTotalMap().forEach((k, v)->{
                if (k == TerminalType.pc) {
                    userPcNumLabel.setText(v + "");
                } else if (k == TerminalType.android) {
                    userAndroidNumLabel.setText(v + "");
                } else if (k == TerminalType.ios) {
                    userIosNumLabel.setText(v + "");
                } else if (k == TerminalType.web) {
                    userWebNumLabel.setText(v + "");
                } else if (k == TerminalType.pad) {
                    userPadNumLabel.setText(v + "");
                } else if (k == TerminalType.ipad) {
                    userIPadNumLabel.setText(v + "");
                } else if (k == TerminalType.hm) {
                    userHmNumLabel.setText(v + "");
                }
            });
        });
    }
}
